#!/usr/bin/python
# -*-coding:UTF-8 -*-

# ========================
# @Time  : 2021/3/3
# @Author: Sunny
# ========================

"""
四个进程发送100个请求
"""
from multiprocessing import Process, Queue
import requests
import time


# 定义装饰器
def decorator(func):
    def wrapper(*args):
        # 获取程序执行前的时间
        start = time.time()
        # 执行功能程序
        func(*args)
        # 获取程序执行后的时间
        end = time.time()
        print(f'程序的执行时间为:{end - start}')
        return end - start

    return wrapper


class MyProcess(Process):
    """自定义函数类"""

    def __init__(self, q):
        super().__init__()
        self.q = q

    # 重写父类的run方法
    def run(self):
        """线程执行的任务函数"""
        while self.q.qsize() > 0:
            url = self.q.get()
            requests.post(url=url, json={"username": "bessky", "password": "123"})
            print(f'{self}-{url}发送请求')


@decorator
def main():
    q = Queue()
    for i in range(50):
        url = 'http://192.168.96.73:8080/login/'
        q.put(url)

    q_list = []
    for i in range(5):
        p = MyProcess(q)
        q_list.append(p)
        p.start()

    for q in q_list:
        q.join()


if __name__ == '__main__':
    main()
